package com.lishe.lishe.lishe.utils;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

/**
	 * 继承 CountDownTimer 防范 重写 父类的方法 onTick() 、 onFinish()
	 */
public class DefaultCountDownTimer extends CountDownTimer {

		private static final int SECONDS = 60; // 秒数
		private static final int MINUTES = 60 * 60; // 小时
		private long first = 0, twice = 0, third = 0;
		private long mtmp = 0, mtmp2 = 0;
		private TextView mTvTime;
		private Finish mFinish;
		private String mFirstText = "";
		private String mLastText = "";
		private boolean isForOrderDetail;

		public interface Finish {
			 void onFinish();
		}

		public void setForOrderDetail(boolean isForOrderDetail) {
			this.isForOrderDetail = isForOrderDetail;
		}
		
		/**
		 * @param millisInFuture
		 *            表示以毫秒为单位 倒计时的总数
		 * @param countDownInterval
		 *            表示 间隔 多少微秒 调用一次 onTick 方法
		 */
		public DefaultCountDownTimer(TextView time, String firsttext, String lasttext, long millisInFuture, long countDownInterval, Finish finish) {
			super(millisInFuture, countDownInterval);
			this.mFinish = finish;
			this.mTvTime = time;
			this.mFirstText = firsttext;
			this.mLastText = lasttext;
		}

		@Override
		public void onFinish() {
			if (mFinish != null) mFinish.onFinish();
		}

		@SuppressLint("ResourceAsColor")
		@Override
		public void onTick(long millisUntilFinished) {
			// 获取当前时间总秒数
			first = millisUntilFinished / 1000;
			if (first < SECONDS) { 			// 小于一分钟 只显示秒
				if (isForOrderDetail) {
					SpannableStringBuilder spannable = new SpannableStringBuilder(mFirstText + "00:" + (first < 10 ? "0" + first : first) + mLastText);
					ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff4138"));
					//设置文字的前景色
					spannable.setSpan(colorSpan, mFirstText.length(),mFirstText.length() + 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					mTvTime.setText(spannable);
				} else {
					mTvTime.setText(mFirstText + "00:" + (first < 10 ? "0" + first : first) + mLastText);
				}
			} else if (first < MINUTES) { 	// 大于或等于一分钟，但小于一小时，显示分钟
				twice = first % 60; 		// 将秒转为分钟取余，余数为秒
				mtmp = first / 60; 			// 将秒数转为分钟
				if (twice == 0) {
					if (isForOrderDetail) {
						SpannableStringBuilder spannable = new SpannableStringBuilder(mFirstText + (mtmp < 10 ? "0" + mtmp : mtmp) + ":00" + mLastText);
						ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff4138"));
						//设置文字的前景色
						spannable.setSpan(colorSpan, mFirstText.length(),mFirstText.length() + 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
						mTvTime.setText(spannable);
					} else {
						mTvTime.setText(mFirstText  + (mtmp < 10 ? "0" + mtmp : mtmp) + ":00" + mLastText);
					}
				} else {
					if (isForOrderDetail) {
						SpannableStringBuilder spannable = new SpannableStringBuilder(mFirstText + (mtmp < 10 ? "0" + mtmp : mtmp) + ":" + (twice < 10 ? "0" + twice : twice) + mLastText);
						ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff4138"));
						//设置文字的前景色
						spannable.setSpan(colorSpan, mFirstText.length(),mFirstText.length() + 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
						mTvTime.setText(spannable);
					} else {
						mTvTime.setText(mFirstText + (mtmp < 10 ? "0" + mtmp : mtmp) + ":" + (twice < 10 ? "0" + twice : twice) + mLastText);
					}
				}
			} else {
				twice = first % 3600; // twice为余数 如果为0则小时为整数
				mtmp = first / 3600;
				if (twice == 0) {
					// 只剩下小时
					if (isForOrderDetail) {
						SpannableStringBuilder spannable = new SpannableStringBuilder(mFirstText + "0" + first / 3600 + ":00:00" + mLastText);
						ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff4138"));
						//设置文字的前景色
						spannable.setSpan(colorSpan, mFirstText.length(),mFirstText.length() + 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
						mTvTime.setText(spannable);
					} else {
						mTvTime.setText(mFirstText + "0" + first / 3600 + ":00:00" + mLastText);
					}
				} else {
					if (twice < SECONDS) { // twice小于60 为秒
						if (isForOrderDetail) {
							SpannableStringBuilder spannable = new SpannableStringBuilder(mFirstText + (mtmp < 10 ? "0" + mtmp : mtmp) + ":00:" + (twice < 10 ? "0" + twice : twice) + mLastText);
							ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff4138"));
							//设置文字的前景色
							spannable.setSpan(colorSpan, mFirstText.length(),mFirstText.length() + 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
							mTvTime.setText(spannable);
						} else {
							mTvTime.setText(mFirstText + (mtmp < 10 ? "0" + mtmp : mtmp) + ":00:" + (twice < 10 ? "0" + twice : twice) + mLastText);
						}
					} else {
						third = twice % 60; // third为0则剩下分钟 否则还有秒
						mtmp2 = twice / 60;
						if (third == 0) {
							if (isForOrderDetail) {
								SpannableStringBuilder spannable = new SpannableStringBuilder(mFirstText + (mtmp < 10 ? "0" + mtmp : mtmp) + ":" + (mtmp2 < 10 ? "0" + mtmp2 : mtmp2) + ":00" + mLastText);
								ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff4138"));
								//设置文字的前景色
								spannable.setSpan(colorSpan, mFirstText.length(),mFirstText.length() + 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
								mTvTime.setText(spannable);
							} else {
								mTvTime.setText(mFirstText + (mtmp < 10 ? "0" + mtmp : mtmp) + ":" + (mtmp2 < 10 ? "0" + mtmp2 : mtmp2) + ":00" + mLastText);
							}
						} else {
							if (isForOrderDetail) {
								SpannableStringBuilder spannable = new SpannableStringBuilder(mFirstText + (mtmp < 10 ? "0" + mtmp : mtmp) + ":" + (mtmp2 < 10 ? "0" + mtmp2 : mtmp2) + ":" + (third < 10 ? "0" + third : third)  + mLastText);
								ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff4138"));
								//设置文字的前景色
								spannable.setSpan(colorSpan, mFirstText.length(),mFirstText.length() + 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
								mTvTime.setText(spannable);
							} else {
								mTvTime.setText(mFirstText + (mtmp < 10 ? "0" + mtmp : mtmp) + ":" + (mtmp2 < 10 ? "0" + mtmp2 : mtmp2) + ":" + (third < 10 ? "0" + third : third)  + mLastText);
							}
						}
					}
				}
			}
		}
	}